local addonName, addon = ...

local ldb = LibStub:GetLibrary("LibDataBroker-1.1", true)
local icon = LibStub:GetLibrary("LibDBIcon-1.0", true)

local frameName = "我的配置中心";

-- 1.创建小地图图标
-- type = "data source"
-- type = "launcher"
local plugin = ldb:NewDataObject(addonName, {
    type = "data source",
    icon = "Interface\\AddOns\\abcInterface\\Textures\\logo",
    OnClick = function(self, button)
        --addon:debug(button)
        if button == "LeftButton" then
            if IsShiftKeyDown() then
                addon:debug("shift + left")
            elseif IsAltKeyDown() then
                addon:debug("alt + left")
            elseif IsControlKeyDown() then
                addon:debug("ctrl + left")
            else
                addon:debug("left")
                --GameTooltip:Hide();
                --ReloadUI()
                if InterfaceOptionsFrame_OpenToCategory then
                    addon:debug("11111111111111111")
                    InterfaceOptionsFrame_OpenToCategory(frameName)
                else
                    addon:debug("22222222222")
                    Settings.OpenToCategory(addon.settingsCategory.ID)
                end
            end
        elseif button == "RightButton" then
            addon:debug("right")
            ReloadUI()
            --InterfaceOptionsFrame_OpenToCategory(addonName)
            --InterfaceOptionsFrame_OpenToCategory(frameName)
            --Settings.OpenToCategory(addon.settingsCategory.ID)
        end
    end,
    OnTooltipShow = function(tooltip)
        --addon:debug(tooltip)
        tooltip:AddLine(frameName)
        --tooltip:AddLine(frameName .. ", " .. "随时随地开发更新!", 1, 1, 1, false)
        tooltip:AddLine("|cff1eff00" .. frameName .. ", " .. "随时随地开发更新!" .. ":|r", 1, 1, 1, false)
    end,
})
-- #####################################################################################################################
if not minimap_DB then
    minimap_DB = {}  -- minimap_DB就是 ## SavedVariables: minimap_DB 中
    minimap_DB.minimapPos = 191 -- 定义小地图图标位置
end
-- #####################################################################################################################
-- 2.注册(核心逻辑--创建Frame)
icon:Register(addonName, plugin, minimap_DB)
-- #####################################################################################################################
-- 3.注册在选项中
local mainFrame = CreateFrame("Frame", frameName, nil, InterfaceOptionsFramePanelContainer, nil)
mainFrame:Hide();
--InterfaceOptions_AddCategory(mainFrame)
local category, layout = Settings.RegisterCanvasLayoutCategory(mainFrame, mainFrame:GetName());
category.ID = mainFrame:GetName()
--local category, layout = Settings.RegisterVerticalLayoutCategory(mainFrame.name);
Settings.RegisterAddOnCategory(category);
-- 3.1 布局
mainFrame:SetScript("OnShow", function(frame)
    --------------------------------------------------------------------------------------------------------------------------
    -- 311:标题
    local title = frame:CreateFontString(nil, "ARTWORK", "GameFontNormalLarge")
    title:SetPoint("TOPLEFT", 16, -16)
    title:SetText(frameName)
    --------------------------------------------------------------------------------------------------------------------------
    local reloadButton = CreateFrame("Button", frameName .. "_ReloadButton", frame, "UIPanelButtonTemplate")
    reloadButton:SetText("重载生效")
    reloadButton:SetSize(120, 25)
    reloadButton:SetEnabled(true)
    reloadButton:SetScript("OnClick", function()
        ReloadUI()
    end)
    reloadButton:SetPoint("TOPLEFT", title, "TOPLEFT", title:GetWidth() + 16, 0)
    --------------------------------------------------------------------------------------------------------------------------
    local showMiniMap = addon:newCheckbox(mainFrame, "小地图按钮", "小地图按钮", function(self, value)
        if value then
            icon:Show(addonName)
        else
            icon:Hide(addonName)
        end
    end)
    showMiniMap:SetPoint("TOPLEFT", title, "BOTTOMLEFT", -2, -16)
    showMiniMap:SetChecked(true)
    --------------------------------------------------------------------------------------------------------------------------
    local showLuaError = addon:newCheckbox(mainFrame, "展示lua错误", "展示lua错误", function(self, value)
        print("scriptErrors old = ", GetCVar("scriptErrors"));
        if value == true then
            SetCVar("scriptErrors", "1")
        else
            SetCVar("scriptErrors", "0")
        end
        print("scriptErrors new = ", GetCVar("scriptErrors"));
    end)
    showLuaError:SetPoint("TOPLEFT", showMiniMap, "BOTTOMLEFT", -2, -16)
    local scriptErrors = GetCVar("scriptErrors")
    if scriptErrors == "1" then
        showLuaError:SetChecked(true)
    else
        showLuaError:SetChecked(false)
    end
    --------------------------------------------------------------------------------------------------------------------------
    --- 312:反和谐开关
    local showBlood = addon:newCheckbox(mainFrame, "反和谐开关", "反和谐开关", function(self, value)
        --print(value);
        --SET overrideArchive "0"
        print("overrideArchive old = ", GetCVar("overrideArchive"));
        if value == true then
            SetCVar("overrideArchive", "0")
        else
            SetCVar("overrideArchive", "1")
        end
        print("overrideArchive new = ", GetCVar("overrideArchive"));
    end)
    showBlood:SetPoint("TOPLEFT", showLuaError, "BOTTOMLEFT", -2, -16)
    local overrideArchive = GetCVar("overrideArchive")
    if overrideArchive == "0" then
        showBlood:SetChecked(true)
    else
        showBlood:SetChecked(false)
    end
    --------------------------------------------------------------------------------------------------------------------------
    --- 313:敏感词开关
    local sensitiveWord = addon:newCheckbox(mainFrame, "敏感词开关", "敏感词开关", function(self, value)
        --print(value);
        --/console SET profanityFilter "0"
        print("profanityFilter old = ", GetCVar("profanityFilter"));
        if value == true then
            SetCVar("profanityFilter", "0")
        else
            SetCVar("profanityFilter", "1")
        end
        print("profanityFilter new = ", GetCVar("profanityFilter"));
    end)
    sensitiveWord:SetPoint("TOPLEFT", showBlood, "BOTTOMLEFT", 0, -16)
    local profanityFilter = GetCVar("profanityFilter")
    if profanityFilter == "0" then
        sensitiveWord:SetChecked(true)
    else
        sensitiveWord:SetChecked(false)
    end
    --------------------------------------------------------------------------------------------------------------------------
    --- 314:动作条狮鹫
    local griffonOfActionBar = addon:newCheckbox(mainFrame, "动作条狮鹫隐藏【10.0 新UI在编辑模式中提供了隐藏开关】", "动作条狮鹫隐藏", function(self, value)
        -- print(value);
        -- NOTE(ljs):10.0 新UI在编辑模式中提供了隐藏开关
        -- MainMenuBarLeftEndCap:Hide()
        -- MainMenuBarRightEndCap:Hide()
    end)
    griffonOfActionBar:SetPoint("TOPLEFT", sensitiveWord, "BOTTOMLEFT", 0, -16)
    griffonOfActionBar:SetChecked(true)
    griffonOfActionBar:SetEnabled(false)
    --------------------------------------------------------------------------------------------------------------------------
    --- 315:视角最大化
    local cameraDistanceMaxZoomFactor = addon:newCheckbox(mainFrame, "视角最大化", "视角最大化", function(self, value)
        --print(value);
        -- 超远视距(默认是2.6)
        -- /console cameraDistanceMaxFactor 2.6     -- 旧的不好用了
        -- /console cameraDistanceMaxZoomFactor 2.6
        print("cameraDistanceMaxZoomFactor old = ", GetCVar("cameraDistanceMaxZoomFactor"));

        if value == true then
            SetCVar("cameraDistanceMaxFactor", 2.6)
            SetCVar("cameraDistanceMaxZoomFactor", 2.6)
        else
            SetCVar("cameraDistanceMaxFactor", 1)
            SetCVar("cameraDistanceMaxZoomFactor", 1)
        end
        print("cameraDistanceMaxZoomFactor new = ", GetCVar("cameraDistanceMaxZoomFactor"));
    end)
    cameraDistanceMaxZoomFactor:SetPoint("TOPLEFT", griffonOfActionBar, "BOTTOMLEFT", 0, -16)
    SetCVar("cameraDistanceMaxZoomFactor", 2.6)
    cameraDistanceMaxZoomFactor:SetChecked(true)
    cameraDistanceMaxZoomFactor:SetEnabled(false)
    --------------------------------------------------------------------------------------------------------------------------
    --- 316:幻化
    local canIMogIt = addon:newCheckbox(mainFrame, "开启查看跨甲跨武器的外观是否被收藏", "开启查看跨甲跨武器的外观是否被收藏", function(self, value)
        -- print(value);
        -- 显示1:你尚未收藏过此外观
        -- 显示2:你拥有此外观,但不是来自此物品
        -- /console missingTransmogSourceInItemTooltips 1
        print("missingTransmogSourceInItemTooltips old = ", GetCVar("missingTransmogSourceInItemTooltips"));

        if value == true then
            SetCVar("missingTransmogSourceInItemTooltips", "1")
        else
            SetCVar("missingTransmogSourceInItemTooltips", "0")
        end
        print("missingTransmogSourceInItemTooltips new = ", GetCVar("missingTransmogSourceInItemTooltips"));
    end)
    canIMogIt:SetPoint("TOPLEFT", cameraDistanceMaxZoomFactor, "BOTTOMLEFT", 0, -16)
    local missingTransmogSourceInItemTooltips = GetCVar("missingTransmogSourceInItemTooltips")
    if missingTransmogSourceInItemTooltips == "1" then
        canIMogIt:SetChecked(true)
    else
        canIMogIt:SetChecked(false)
    end
    --------------------------------------------------------------------------------------------------------------------------
    --- 317:装备来源
    local equipSourceInfo = addon:newCheckbox(mainFrame, "显示装备来源", "显示装备来源", function(self, value)
        -- print(value);
        -- 来源: 群星庭院 巡逻对账加多
    end)
    equipSourceInfo:SetPoint("TOPLEFT", canIMogIt, "BOTTOMLEFT", 0, -16)
    --------------------------------------------------------------------------------------------------------------------------
    --- 318:装备来源
    local equipSetInfo = addon:newCheckbox(mainFrame, "显示套装信息", "显示套装信息", function(self, value)
        -- print(value);
    end)
    equipSetInfo:SetPoint("TOPLEFT", equipSourceInfo, "BOTTOMLEFT", 0, -16)
    --------------------------------------------------------------------------------------------------------------------------
    local professionInfo = addon:newCheckbox(mainFrame, "专业技能优化", "专业技能优化", function(self, value)
        -- print(value);
    end)
    professionInfo:SetPoint("TOPLEFT", equipSetInfo, "BOTTOMLEFT", 0, -16)
    --------------------------------------------------------------------------------------------------------------------------
    local loot = CreateFrame("Button", nil, frame, "UIPanelButtonTemplate")
    loot:SetText("战利品掷骰 ROLL点 or /loot")
    loot:SetSize(250, 25)
    loot:SetEnabled(true)
    loot:SetPoint("TOPLEFT", professionInfo, "BOTTOMLEFT", -2, -16)
    loot:SetScript("OnClick", function()

    end)
    loot:SetScript("OnClick", function(self, button, down)
        print("OnClick()-->button = ", button, ", down = ", down);
        if button == 'LeftButton' then
            ToggleLootHistoryFrame()
        else
            RandomRoll("1", "100");
        end
    end)
    --------------------------------------------------------------------------------------------------------------------------
    local testApi = addon:newCheckbox(mainFrame, "测试API", "测试API", function(self, value)
        print("scriptErrors = ", GetCVar("scriptErrors"))
        print("overrideArchive = ", GetCVar("overrideArchive"))
        print("profanityFilter = ", GetCVar("profanityFilter"))
        print("portal = ", GetCVar("portal"))
        print("missingTransmogSourceInItemTooltips = ", GetCVar("missingTransmogSourceInItemTooltips"))
        print("cameraDistanceMaxFactor = ", GetCVar("cameraDistanceMaxFactor"))
        print("cameraDistanceMaxZoomFactor = ", GetCVar("cameraDistanceMaxZoomFactor"))
    end)
    testApi:SetPoint("TOPLEFT", loot, "BOTTOMLEFT", 0, -16)
    --------------------------------------------------------------------------------------------------------------------------
end)
-- 3.2 监听
mainFrame:RegisterEvent("ADDON_LOADED")
mainFrame:SetScript("OnEvent", function(self, event, loadedAddon)
    if event == "ADDON_LOADED" and loadedAddon == addonName then

    end
end)
-- #####################################################################################################################
-- 3.3 添加子目录
--local panel = CreateFrame("Frame")
--Settings.RegisterCanvasLayoutSubcategory(category, panel, "系统");
--Settings.RegisterVerticalLayoutSubcategory(category, "系统");
--Settings.RegisterCanvasLayoutSubcategory(category, panel, "任务");
--Settings.RegisterCanvasLayoutSubcategory(category, panel, "专业")
--Settings.RegisterCanvasLayoutSubcategory(category, panel, "角色信息")
--Settings.RegisterCanvasLayoutSubcategory(category, panel, "地图")
--Settings.RegisterCanvasLayoutSubcategory(category, panel, "聊天框")
--Settings.RegisterVerticalLayoutSubcategory(category, "专业")
-- #####################################################################################################################
function addon:InterfaceOptions_AddSubCategory(tabFrame, tabName)
    if tabFrame then
        return Settings.RegisterCanvasLayoutSubcategory(category, tabFrame, tabName)
    else
        return Settings.RegisterVerticalLayoutSubcategory(category, tabName) -- Blizzard_SettingsInbound.lua
    end
end
-- #####################################################################################################################